package cn.hua.trip.app.base.controller;

import cn.hua.trip.app.base.util.UserContext;
import cn.hua.trip.base.domain.Travel;
import cn.hua.trip.base.query.TravelCommendQueryObject;
import cn.hua.trip.base.query.TravelQueryObject;
import cn.hua.trip.base.service.ITravelCommendService;
import cn.hua.trip.base.service.ITravelService;
import cn.hua.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelCommendService travelCommendService;

    //保存游记操作
    @PostMapping
    public Object save(Travel travel){
        JSONResult result = new JSONResult();
        try {
            travel.setAuthor(UserContext.getCurrentUser());
            travelService.save(travel);
        }catch (Exception e){
            result.mark(e.getMessage());
        }
        return result;
    }

    //通过id查询单条的游记信息,用于编辑页面的回显操作
    @GetMapping("/{id}")
    public Object get(@PathVariable Long id){
        return travelService.get(id);
    }


    //查询所有的游记信息
    @GetMapping
    public Object list(TravelQueryObject qo){
        //设置状态为已经发布的
        qo.setState(Travel.STATE_RELEASE);
        //设置是公开的
        qo.setIsPublic(true);

        //按照发布时间来排序
        if (qo.getOrderBy()==null){
            qo.setOrderBy("t.releaseTime desc");
        }
         return travelService.queryForList(qo);
    }

    //更新游记
    @PutMapping
    public Object update(Travel travel){

        JSONResult result = new JSONResult();
        try {
            travelService.update(travel);
        }catch (Exception e){
            result.mark(e.getMessage());
        }
        return result;

    }


    /*查询的是游记的推荐 可以传入type,type=1就是周推荐  type=2就是月推荐  type=3就是攻略推荐*/
    @GetMapping("/travelCommends")
    public Object getCommendsByType(TravelCommendQueryObject qo){

        PageInfo<Map> info = travelCommendService.queryForListApp(qo);
        return info;
    }
}
